var app = getApp();
Page(Object.assign({},{
    data:{
        options : null,
        inited : true,
        checked : false,
        info : {},
		siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
    },
    onLoad:function(options){
        var self = this;
        self.setData({
            options : options,
        });
        var id = options.id ? options.id : 0;
        app.com.auth(function(user){
            app.com.http('admin','POST',{op:'addsortstart',id:id},0,false,'','',function(res){
                if (!res.data.errno) {
                    self.setData({
                        info : res.data.data.info ? res.data.data.info : {},
                        checked : res.data.data.info && res.data.data.info.status == 1 ? false : true,
                        inited : true,
                    });
                    // 关闭下拉刷新
                    wx.stopPullDownRefresh();
                }else{
                    app.com.alert( res.data.message,function(){
                        if( res.data.errno == 2 ){
                            app.globalData.adminurl = app.com.getUrlArgs();
                            wx.navigateTo({
                                url : res.data.data.url
                            });
                        }else{
                            wx.navigateBack();
                        }
                    });
                }
            });
        });
        wx.setNavigationBarTitle({
          title: '添加分类',
        });
    },
    pub(){
        var self = this;
        self.savedata();
    },
    savedata(){
        var self = this;

        self.data.info.status = self.data.checked ? 0 : 1;

        var form = JSON.stringify( self.data.info );
        app.com.http('admin','POST',{op:'pubgoodsort',form:form},0,true,'','',function(res){
            if (!res.data.errno) {
                app.com.toast(res.data.message,'',function(){
                    wx.navigateBack();
                });
            }else{
                app.com.toast( res.data.message,'none' );
                if( res.data.errno == 2 ){
                    wx.navigateTo({
                        url : res.data.data.url
                    });
                }
            }
        });
    },
    afterRead(e){
        var self = this;
        wx.showLoading({mask:true,title:'上传中'});
        wx.uploadFile({
            url: app.siteInfo.siteroot+'?i='+app.siteInfo.uniacid+'&j=&c=utility&a=file&do=upload&type=image&thumb=0',
            filePath: e.detail.file.path,
            name: 'file',
            success: function(res){
                var aaa = JSON.parse( res.data );
                self.setData({
                    'info.img' : aaa.attachment,
                    'info.showimg' : aaa.url
                })
            },
            complete : function(){
                wx.hideLoading();
            },
            fail : function(rr){
                console.log(rr)
            }
        })
    },
    onChange(e){
        var keys = e.currentTarget.dataset.type;
        var key = 'info.'+keys;
        this.setData({
            [key] : e.detail
        })
    },
    changestatus(){
        this.setData({
            checked : !this.data.checked
        })
    },
    delimg(){
        this.setData({
            'info.img' : '',
            'info.showimg' : '',
        })
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    
}))